package com.baihan.angus.app.game.common.util;

import android.text.TextUtils;
import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtils {

    private static GregorianCalendar calendar = new GregorianCalendar();

    // 提供“yyyy-mm-dd”形式的字符串到毫秒的转换
    public static long getMillis(String dateString) {
        String[] date = dateString.split("-");
        return getMillis(date[0], date[1], date[2]);
    }

    // 提供“yyyy-mm-dd hh:mm:ss”形式的字符串到毫秒的转换
    public static long getMillis2(String dateString) {
        String[] date = dateString.substring(0, dateString.indexOf(" ")).split("-");
        String s2 = dateString.substring(dateString.indexOf(" ") + 1);
        String[] date2 = s2.split(":");
        return getMillis2(date[0], date[1], date[2], date2[0], date2[1], date2[2]);
    }

    // 根据输入的年、月、日，转换成毫秒表示的时间
    public static long getMillis(int year, int month, int day) {
        GregorianCalendar calendar = new GregorianCalendar(year, month, day);
        return calendar.getTimeInMillis();

    }

    // 根据输入的年、月、日、时、分、秒转换成毫秒表示的时间
    public static long getMillis2(int year, int month, int day, int hour, int minute, int second) {
        GregorianCalendar calendar = new GregorianCalendar(year, month, day, hour, minute, second);
        return calendar.getTimeInMillis();

    }

    // 根据输入的年、月、日，转换成毫秒表示的时间
    public static long getMillis(String yearString, String monthString,
                                 String dayString) {
        int year = Integer.parseInt(yearString);
        int month = Integer.parseInt(monthString) - 1;
        int day = Integer.parseInt(dayString);
        return getMillis(year, month, day);
    }

    // 根据输入的时、分、秒，转换成毫秒表示的时间
    public static long getMillis2(String yearString, String monthString,
                                  String dayString, String hour, String minute,
                                  String second) {
        int year = Integer.parseInt(yearString);
        int month = Integer.parseInt(monthString) - 1;
        int day = Integer.parseInt(dayString);
        int h = Integer.parseInt(hour);
        int m = Integer.parseInt(minute);
        int s = Integer.parseInt(second);
        return getMillis2(year, month, day, h, m, s);
    }

    // 获得当前时间的毫秒表示
    public static long getNow() {
        GregorianCalendar now = new GregorianCalendar();
        return now.getTimeInMillis();

    }

    // 根据输入的毫秒数，获得日期字符串
    public static String getDate(long millis) {
        calendar.setTimeInMillis(millis);
        return formatDate3("yyyy-MM-dd HH:mm:ss", calendar.getTime());
    }

    // 根据输入的毫秒数，获得日期字符串
    public static String getDate1(long millis) {
        calendar.setTimeInMillis(millis * 1000);
        return formatDate3("yyyy-MM-dd HH:mm", calendar.getTime());
    }

    public static String getTime(Date date) {//可根据需要自行截取数据显示
        Log.d("getTime()", "choice date millis: " + date.getTime());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    // 根据输入的毫秒数，获得年份
    public static int getYear(long millis) {
        calendar.setTimeInMillis(millis);
        return calendar.get(Calendar.YEAR);

    }

    // 根据输入的毫秒数，获得月份
    public static int getMonth(long millis) {
        calendar.setTimeInMillis(millis);
        return calendar.get(Calendar.MONTH);

    }

    // 根据输入的毫秒数，获得日期
    public static int getDay(long millis) {
        calendar.setTimeInMillis(millis);
        return calendar.get(Calendar.DATE);

    }

    // 根据输入的毫秒数，获得小时
    public static int getHour(long millis) {
        calendar.setTimeInMillis(millis);
        return calendar.get(Calendar.HOUR_OF_DAY);

    }

    // 根据输入的毫秒数，获得分钟
    public static int getMinute(long millis) {
        calendar.setTimeInMillis(millis);
        return calendar.get(Calendar.MINUTE);

    }

    // 根据输入的毫秒数，获得秒
    public static int getSecond(long millis) {
        calendar.setTimeInMillis(millis);
        return calendar.get(Calendar.SECOND);

    }

    //获取指定毫秒数的对应星期
    public static String getWeek(long millis) {
        calendar.setTimeInMillis(millis);
        String week = "";
        int cweek = calendar.get(Calendar.DAY_OF_WEEK);
        switch (cweek) {
            case 1:
                week = "日";
                break;
            case 2:
                week = "一";
                break;
            case 3:
                week = "二";
                break;
            case 4:
                week = "三";
                break;
            case 5:
                week = "四";
                break;
            case 6:
                week = "五";
                break;
            case 7:
                week = "六";
                break;
        }
        return week;

    }

    // 获得今天日期
    public static String getTodayData() {
        return getDate(getNow());
    }

    // 获得明天日期
    public static String getTomoData() {
        // 86400000为一天的毫秒数
        return getDate(getNow() + 86400000);

    }

    // 获得后天日期
    public static String getTheDayData() {
        return getDate(getNow() + 86400000 + 86400000);
    }

    // 获得昨天日期
    public static String getYesData() {
        return getDate(getNow() - 86400000L);
    }

    // 获得前天日期
    public static String getBeforeYesData() {
        return getDate(getNow() - 86400000L - 86400000L);
    }


    public static String formatDate(String format, long date) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date d = new Date(date);
        String newDate = sdf.format(d);
        return newDate;
    }

    /**
     * 将时间戳转化成固定格式（默认 yyyy-MM-dd HH:mm:ss 当前时间 ）
     */
    public static String formatDate2(String format, String time) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = formatter.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return String.valueOf(android.text.format.DateFormat.format(format, date));
    }

    public static String formatDate3(String format, Date date) {

        if (TextUtils.isEmpty(format)) {
            format = "yyyy-MM-dd HH:mm:ss";
        }

        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String newDate = sdf.format(date);
        return newDate;
    }


    /**
     * 获取今天时间具体内容
     *
     * @return
     */
    public static String StringData() {
        final Calendar c = Calendar.getInstance();
//        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        String mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
        String mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
        String mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
        String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
        if ("1".equals(mWay)) {
            mWay = "天";
        } else if ("2".equals(mWay)) {
            mWay = "一";
        } else if ("3".equals(mWay)) {
            mWay = "二";
        } else if ("4".equals(mWay)) {
            mWay = "三";
        } else if ("5".equals(mWay)) {
            mWay = "四";
        } else if ("6".equals(mWay)) {
            mWay = "五";
        } else if ("7".equals(mWay)) {
            mWay = "六";
        }
        return mYear + "年" + mMonth + "月" + mDay + "日" + " 星期" + mWay;
    }

    /**
     * 获取之前的日期
     * @param distanceDay
     * @return
     */
    public static String getOldDate(int distanceDay) {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate = new Date();
        Calendar date = Calendar.getInstance();
        date.setTime(beginDate);
        date.set(Calendar.DATE, date.get(Calendar.DATE) - distanceDay);
        Date endDate = null;
        try {
            endDate = dft.parse(dft.format(date.getTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dft.format(endDate);
    }

    /**
     * 获取类似今天、昨天的简单名称
     *
     * @param date 格式为 20xx-xx-xx
     * @return
     */
    public static String getCustomStr(String date) {
        if (getMillis(date) == getMillis(getTodayData())) {
            return "今天 " + getCustomStr7(date);
        } else {
            return getCustomStr7(date);
        }
    }

    /**
     * 获取类似今天、昨天的简单名称
     *
     * @param date 格式为 20xx-xx-xx
     * @return
     */
    public static String getCustomStr2(String date) {
        if (getMillis2(date) == getMillis2(getYesData())) {
            return "昨天";
        } else if (getMillis2(date) == getMillis(getTodayData())) {
            long a = getNow() - getMillis2(date);
            float h = a * 1.0f / (1000 * 60 * 60);
            if (h < 1) {
                float m = a * 1.0f / (1000 * 60);
                if (m < 1) {
                    return "刚刚";
                } else {
                    return (int) m + "分钟前";
                }
            } else {
                return (int) h + "小时前";
            }
        } else {
            return date.substring(0, date.indexOf(" "));
        }
    }

    /**
     * 获取类似今天、昨天的简单名称
     *
     * @param date 格式为 20xx-xx-xx
     * @return
     */
    public static String getCustomStr3(String date) {
        if (getMillis2(date) == getMillis2(getTodayData())) {
            return "今天 " + getCustomStr6(date);
        } else {
            return getCustomStr5(date);
        }
    }

    /**
     * 获取 yy月dd日
     *
     * @param date
     * @return
     */
    public static String getCustomStr4(String date) {
        return formatDate2("MM月dd日", date);
    }

    /**
     * 获取 yy月dd日 hh:mm
     *
     * @param date
     * @return
     */
    public static String getCustomStr5(String date) {
        return formatDate2("MM/dd HH:mm", date);
    }

    /**
     * 获取 hh:mm
     *
     * @param date
     * @return
     */
    public static String getCustomStr6(String date) {
        return formatDate2("HH:mm", date);
    }

    /**
     * 获取 yy年mm月dd日
     *
     * @param date
     * @return
     */
    public static String getCustomStr7(String date) {
        return formatDate2("yyyy年MM月dd日", date);
    }

    /**
     * 获取 yy/mm/dd
     *
     * @param date
     * @return
     */
    public static String getCustomStr8(String date) {
        return formatDate2("yyyy/MM/dd HH:mm", date);
    }
    /**
     * 获取 yy年mm月dd日 星期
     *
     * @return
     */
    public static String getCustomStr9(String date) {
        final Calendar c = Calendar.getInstance();
        c.setTimeInMillis(getMillis(date));
        String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
        if ("1".equals(mWay)) {
            mWay = "日";
        } else if ("2".equals(mWay)) {
            mWay = "一";
        } else if ("3".equals(mWay)) {
            mWay = "二";
        } else if ("4".equals(mWay)) {
            mWay = "三";
        } else if ("5".equals(mWay)) {
            mWay = "四";
        } else if ("6".equals(mWay)) {
            mWay = "五";
        } else if ("7".equals(mWay)) {
            mWay = "六";
        }
        return getCustomStr6(formatDate3(null, c.getTime())) + " 周" + mWay;
    }

}
